import {getToken} from '@/utils/myToken'
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import Userinfo from '../views/Userinfo.vue'
import Useredit from '../views/Useredit.vue'
import Attention from '../views/Attention.vue'
import Collect from '../views/Collect.vue'
import Detail from '../views/Detail.vue'
import Comment from '../views/Comment.vue'
import UserComment from '../views/UserComment.vue'
import Search from '../views/Search.vue'
import Manage from '../views/Manage.vue'

//测试用页面
// import Test from '../demo/TestFollows.vue'
// import Test from '../demo/TestList.vue'
// import Test from '../demo/TestDiguiPage.vue'
import Test from '../demo/TestCommentDiguiPage.vue'
// import { Search } from 'node_modules/vant/types/index'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  {
    path: '/userinfo',
    name: 'Userinfo',
    component: Userinfo,
    meta: {
      isAuth: true
    }
  },
  {
    path: '/useredit',
    name: 'Useredit',
    component: Useredit,
    meta: {
      isAuth: true
    }
  },
  {
    path:'/attention',
    name:'Attention',
    component: Attention,
    meta: {
      isAuth: true
    }
  },
  {
    path:'/collect',
    name:'Collect',
    component: Collect,
    meta:{
      isAuth:true
    }
  },
  {
    path:'/detail/:id',
    name:'Detail',
    component: Detail
  },
  {
    path:'/comment/:id',
    name:'Comment',
    component:Comment
  },
  {
    path:'/usercomment',
    name:'UserComment',
    component:UserComment,
    meta: { 
      isAuth: true
     }
  },
  {
    path:'/search',
    name:'Search',
    component: Search
  },
  {
    path:'/manage',
    name:'Manage',
    component:Manage
  },
  //测试页面
  {
    path:'/test',
    name:'Test',
    component:Test
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  // console.log(to)
  // console.log(from)
  // console.log(next)
  // if(!getToken() && to.name === 'Userinfo'){
  //   return router.replace('/login')
  // }else if(!getToken() && to.name === 'Useredit'){
  //   return router.replace('/login')
  // }
  if (to.meta.isAuth === true) {
    if (!getToken()) {
      return router.replace('/login')
    }
  }
  next()
})

export default router